package youleche.com.sql.business;

import java.sql.Connection;
import java.util.List;

import youleche.com.sql.factory.ConnectionFactory;
import youleche.com.sql.factory.DaoFactory;
import youleche.com.sql.idao.ITableField;
import youleche.com.sql.model.MTableField;

/**
 * 表字段业务层
 * @author Administrator
 *
 */
public class BTableField {
	/**
	 * 数据库连接
	 */
	private Connection con = null;
	
	/**
	 * 处理器
	 */
	private ITableField handler = null;
	
	/**
	 * 构造器
	 */
	public BTableField(){
		this.handler = DaoFactory.getDTableField();
	}
	
	/**
	 * 根据tableId获取字段
	 * @param tableId 表ID
	 * @return 返回所有字段
	 * @throws Exception 异常
	 */
	public List<MTableField> getTableFieldsByTableId(
			String tableId) throws Exception{
		try{
			this.con = ConnectionFactory.getAutoTableReadConnection();
			return this.handler.getTableFieldsByTableId(this.con, tableId);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 添加表字段
	 * @param tablefield 表字段对象
	 * @return 返回影响的行数
	 * @throws Exception 异常
	 */
	public int addTableFields(List<MTableField> tablefields)
			throws Exception{
		try{
			this.con = ConnectionFactory.getAutoTableWriteConnection();
			int count = 0;
			int maxfieldId = this.handler.getMaxFieldIdByTableId(this.con, tablefields.get(0).getTableId());
			for (MTableField tablefield : tablefields) {
				tablefield.setFieldId(maxfieldId + 1);
				maxfieldId = maxfieldId + 1;
				count += this.handler.addTableFields(this.con, tablefield);
			}
			
			return count;
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 获取最大的字段ID
	 * @param tableId 表ID
	 * @return 返回最大的fieldId
	 * @throws Exception 异常
	 */
	public int getMaxFieldIdByTableId(String tableId) throws Exception{
		try{
			this.con = ConnectionFactory.getAutoTableReadConnection();
			return this.handler.getMaxFieldIdByTableId(this.con, tableId);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 得到备用字段的数量
	 * @return 返回备用字段的数量
	 * @throws Exception 异常
	 */
	public int getReadyFieldNum(String tableId) throws Exception {
		try{
			this.con = ConnectionFactory.getAutoTableReadConnection();
			return this.handler.getReadyFieldNum(this.con, tableId);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 判断是否存在该字段
	 * @param englishName 字段英文名称
	 * @param tableId 表ID
	 * @return 返回是否存在该字段 true-存在
	 * @throws Exception 异常
	 */
	public boolean isExistFieldByFieldEnglishNameAndTableId(
			String englishName, String tableId) throws Exception{
		try{
			this.con = ConnectionFactory.getAutoTableReadConnection();
			return this.handler.isExistFieldByFieldEnglishNameAndTableId(this.con, englishName, tableId);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 通过表ID和英文名称更新字段
	 * @param tablefield 字段信息
	 * @return 返回更新影响的行数
	 * @throws Exception 异常
	 */
	public int updateFieldByIdAndEnglishName(
			MTableField tablefield) throws Exception{
		try{
			this.con = ConnectionFactory.getAutoTableWriteConnection();
			return this.handler.updateFieldByIdAndEnglishName(this.con, tablefield);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
}
